<?php

/**
 * page actions.
 *
 * @package    barilga
 * @subpackage page
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class contentActions extends sfActions
{

    public function executeIndex(sfWebRequest $request)
    {
        
      	$params = array();
      	if($request->getParameter('objectType')) $params['objectType'] = $request->getParameter('objectType');
      	if($request->getParameter('objectId')) $params['objectId'] = $request->getParameter('objectId');
      	if($request->getParameter('categoryId')) $params['categoryId'] = $request->getParameter('categoryId');
      	if($request->getParameter('keyword')) $params['keyword'] = $request->getParameter('keyword');
    	
        $this->pager = Doctrine::getTable('News')->getPager($params, $request->getParameter('page'));
    }
    
    
    public function executeLoadObjects(sfWebRequest $request)
    {
        $objects = array();
        $seasonId = $request->getParameter('seasonId');
        $objectType = $request->getParameter('objectType');
        
        $teamIds = Doctrine::getTable('BbSeasonTeams')->getTeamIds($seasonId);
        switch ($objectType)
        {
        	case 'match': $objects = Doctrine::getTable('BbMatch')->fetchArray(array('seasonId'=>$seasonId)); break;
        	case 'team': $objects = Doctrine::getTable('BbTeams')->fetchArray(array(), $teamIds); break;
        	case 'player': $objects = Doctrine::getTable('BbPlayers')->fetchArray(array(), $teamIds);break;
        }
        if (!sizeof($objects)) return sfView::NONE;
  
        $this->objects = $objects;
        $this->objectId = $request->getParameter('objectId') ? $request->getParameter('objectId') : 0;
    }
    

    public function executeFeatured(sfWebRequest $request)
    {
        $this->forward404Unless($content = Doctrine::getTable('News')->find($request->getParameter('id')));
    
        $content->setIsFeatured($request->getParameter('status'));
        $content->save();
    
        $this->getUser()->setFlash('success', 'Амжилттай хадгалагдлаа.', true);
        $this->redirect($request->getReferer() ? $request->getReferer() : 'content/index');
    }

  
    public function executeNew(sfWebRequest $request)
    {
        $this->form = new NewsForm();
    }
  
    public function executeCreate(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
    
        $this->form = new NewsForm();
    
        $this->processForm($request, $this->form);
    
        $this->setTemplate('new');
    }
  
    public function executeEdit(sfWebRequest $request)
    {
        $this->forward404Unless($content = Doctrine::getTable('News')->find(array($request->getParameter('id'))), sprintf('Object page does not exist (%s).', $request->getParameter('id')));
        $this->form = new NewsForm($content);
    }
  
    public function executeUpdate(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($content = Doctrine::getTable('News')->find(array($request->getParameter('id'))), sprintf('Object page does not exist (%s).', $request->getParameter('id')));
        $this->form = new NewsForm($content);
    
        $this->processForm($request, $this->form);
    
        $this->setTemplate('edit');
    }
  
    public function executeDelete(sfWebRequest $request)
    {
        $this->forward404Unless($content = Doctrine::getTable('News')->find(array($request->getParameter('id'))), sprintf('Object page does not exist (%s).', $request->getParameter('id')));
        try {
          $content->delete();
          $this->getUser()->setFlash('success', 'Амжилттай устлаа.', true);
        }catch (Exception $e){}
        
        $this->redirect($request->getReferer() ? $request->getReferer() : 'content/index');
    }
  
    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
          $content = $form->save();
          
          myTools::resizeImages($content->getImage(), 'news', array(100));
          
          $this->getUser()->setFlash('success', 'Амжилттай хадгалагдлаа.', true);
          $this->redirect('content/index');
        }
    }

}